package com.mlf.observer.demo02;

import java.beans.PropertyChangeListener;
import java.beans.PropertyChangeSupport;

public class Phone {
    private double price;
    private final PropertyChangeSupport support  = new PropertyChangeSupport(this);


    public double getPrice() {
        return price;
    }
    public void setPrice(double price) {
        double oldPrice = this.price;
        this.price = price;
        support.firePropertyChange("price", oldPrice, price);
    }

    public void addPropertyChangeListener(PropertyChangeListener listener) {
        support.addPropertyChangeListener(listener);
    }

    public void removeLisTener(PropertyChangeListener listener) {
        support.removePropertyChangeListener(listener);
    }

    public static void main(String[] args) {
        Phone phone = new Phone();
        // 添加监听器
        phone.addPropertyChangeListener( event ->
                System.out.println("价格改变了"+  event.getOldValue() +" -->"
                        + event.getNewValue()) );
        phone.addPropertyChangeListener( event ->{
            if ((double)event.getNewValue() < 6000){
                System.out.println("手机价格低于6000，请及时处理！");
            }
            if ((double)event.getNewValue() > 10000){
                System.out.println("手机价格高于10000，请及时处理！");
            }
        });

        // 测试
        phone.setPrice(6999);
        System.out.println("------------------");
        phone.setPrice(5999);
        System.out.println("------------------");
        phone.setPrice(11000);
        System.out.println("------------------");
        phone.setPrice(11000);


    }


}
